home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / PcxImagePlugin.py < prev    next >
Encoding:
Text File  |  2000-06-23  |  2.2 KB  |  90 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id: PcxImagePlugin.py,v 1.1.1.1 1998/08/18 13:07:53 sjoerd Exp $
  4. #
  5. # PCX file handling
  6. #
  7. # This format was used by the popular PaintBrush applications, from
  8. # ZSoft, Inc.  It's also support by many MS-DOS and Windows applications,
  9. # including the PaintBrush program in Windows 3.
  10. #
  11. # History:
  12. #    95-09-01 fl    Created
  13. #    96-05-20 fl    Fixed RGB support
  14. #    97-01-03 fl    Fixed 2-bit and 4-bit support
  15. #
  16. # Copyright (c) Secret Labs AB 1997.
  17. # Copyright (c) Fredrik Lundh 1995-97.
  18. #
  19. # See the README file for information on usage and redistribution.
  20. #
  21.  
  22.  
  23. __version__ = "0.1"
  24.  
  25.  
  26. import array
  27. import Image, ImageFile, ImagePalette
  28.  
  29.  
  30. def i16(c):
  31.     return ord(c[0]) + (ord(c[1])<<8)
  32.  
  33. def _accept(prefix):
  34.     return ord(prefix[0]) == 10 and ord(prefix[1]) in [0, 2, 3, 5]
  35.  
  36. class PcxImageFile(ImageFile.ImageFile):
  37.  
  38.     format = "PCX"
  39.     format_description = "Paintbrush"
  40.  
  41.     def _open(self):
  42.  
  43.     # Header
  44.     s = self.fp.read(128)
  45.     if not _accept(s):
  46.         raise SyntaxError, "not a PCX file"
  47.  
  48.     # Image
  49.     box = i16(s[4:]), i16(s[6:]), i16(s[8:])+1, i16(s[10:])+1
  50.     if box[2] <= box[0] or box[3] <= box[1]:
  51.         raise SyntaxError, "bad PCX image size"
  52.  
  53.     # Format
  54.     bits, planes, stride = ord(s[3]), ord(s[65]), i16(s[66:])
  55.     if bits == 1 and planes == 1:
  56.         self.mode = rawmode = "1"
  57.     elif bits == 1 and planes == 2:
  58.         self.mode = "P"
  59.         rawmode = "P;2L"
  60.         self.palette = ImagePalette.raw("RGB", s[16:64])
  61.     elif bits == 1 and planes == 4:
  62.         self.mode = "P"
  63.         rawmode = "P;4L"
  64.         self.palette = ImagePalette.raw("RGB", s[16:64])
  65.     elif bits == 8 and planes == 1:
  66.         self.mode = rawmode = "L"
  67.         # FIXME: hey, this doesn't work with the incremental loader !!!
  68.         # FIXME: should perhaps check version before looking for a palette
  69.         self.fp.seek(-769, 2)
  70.         s = self.fp.read(769)
  71.         if ord(s[0]) == 12:
  72.         self.palette = ImagePalette.raw("RGB", s[1:])
  73.         self.mode = "P"
  74.     elif bits == 8 and planes == 3:
  75.         self.mode = "RGB"
  76.         rawmode = "RGB;L"
  77.     else:
  78.         raise IOError, "unknown PCX mode"
  79.  
  80.     self.size = box[2]-box[0], box[3]-box[1]
  81.  
  82.     self.tile = [("pcx", box, 128, rawmode)]
  83.  
  84. #
  85. # registry
  86.  
  87. Image.register_open("PCX", PcxImageFile, _accept)
  88.  
  89. Image.register_extension("PCX", ".pcx")
  90.